package com.yonyou.ucf.mdf.app.isvdxq.exception.service;

import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.exception.entity.ExceptionEntity;
import com.yonyou.ucf.mdf.app.isvdxq.exception.mapper.ExceptionEntityMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ExceptionEntityService {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionEntityService.class);
    @Autowired
    ExceptionEntityMapperImpl exceptMapper;

    public ExceptionEntity SelectById(String Id){
        return exceptMapper.selectById(Id);
    }
    public List<ExceptionEntity> SelectPage(String tenantId, int m, int n){
        return exceptMapper.selectPage(tenantId, m, n);
    }

    public DataResult insertException(ExceptionEntity exceptionModel, String tenantId) {
        DataResult dataResult = null;
        String id = MyTool.getRandomId();
        try {
            String strUrl = exceptionModel.getExceptionModel().length() > 200 ? exceptionModel.getExceptionModel().substring(0, 200) :
                    exceptionModel.getExceptionModel();
            int insertid = exceptMapper.InsertException(exceptionModel.getException(), strUrl
                    , exceptionModel.getAppVersion(), exceptionModel.getDeviceID()
                    , exceptionModel.getSystemVersion(), exceptionModel.getSystemModel()
                    , exceptionModel.getDeviceBrand(), exceptionModel.getPixels(), exceptionModel.getIp()
                    , exceptionModel.getFk_userID(), id, tenantId, exceptionModel.getSource()
                    , exceptionModel.getRemark());
            if (insertid > 0) {
                dataResult = new DataResult(1, "异常信息提交成功");
            } else {
                dataResult = new DataResult(0, "异常信息提交失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("异常信息提交失败, 原因: {}", e.toString());
            dataResult = new DataResult(0, "异常信息提交失败" + e.getMessage());
        }
        return dataResult;
    }

    public List<ExceptionEntity> SelectAll(String tenantId) {
        return exceptMapper.selectAll(tenantId);
    }
}